package com.ch999.detect.utils;

import android.content.Context;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;

import com.ch999.detect.View.activity.MainActivityApp;
import com.ch999.detect.constant.SpType;
import com.ch999.detect.mode.bean.GoodsBjgz;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by ch999android on 2017/10/12.
 */

public class CommUtils {

//    public List<GoodsBjgz.AttrBean> mAttrBeen;

    public static List<GoodsBjgz.AttrBean> mPhoneAttr = new ArrayList<>();

    public static Map<String, Class> mActivityList;
    /**
     * 多选的机型属性
     */
    public static List<GoodsBjgz.AttrBean> mCheckBoxAttr = new ArrayList<>();

    public static Integer mAttrSize;


    //单例
    private static CommUtils instance;

    public static CommUtils getInstance() {
        if (instance == null) {
            instance = new CommUtils();
        }
        return instance;
    }

    public CommUtils() {
//        mAttrBeen = getmPhoneAttr();
        instance = this;
    }

//    public  List<GoodsBjgz.AttrBean> getmAttrBeen(){
//        return mAttrBeen;
//    }


//    public List<GoodsBjgz.AttrBean> getmPhoneAttr() {
//        return mPhoneAttr;
//    }

//    public void setmPhoneAttr(List<GoodsBjgz.AttrBean> mPhoneAttr) {
//        this.mPhoneAttr = mPhoneAttr;
//    }

    public Map<String, Class> getmActivityList() {
        return mActivityList;
    }

//    public void setmActivityList(Map<String, Class> mActivityList) {
//        this.mActivityList = mActivityList;
//    }

//
//    public List<GoodsBjgz.AttrBean> getCheckBoxAttr() {
//        return mCheckBoxAttr;
//    }
//
//    public void setCheckBoxAttr(List<GoodsBjgz.AttrBean> checkBoxAttr) {
//        mCheckBoxAttr = checkBoxAttr;
//    }


    /**
     * @param pname 检测名称
     * @return
     */
    public Class getAutoDetectActivityClass(String pname) {
        if (mActivityList.containsKey(pname)) {
            return mActivityList.get(pname);
        }
        return null;
    }


    public Intent getAutoDetectActivityIntent(Context context, int posation) {

        GoodsBjgz.AttrBean attrBean = mPhoneAttr.get(posation);

        if (attrBean != null) {

            Class autoDetectActivityClass = getAutoDetectActivityClass(attrBean.getTpname());
            if (autoDetectActivityClass == null) {

                if (SharePrefUtil.getInstance(context).getBoolean(SpType.KEY_IS_POSATION_ZEROR)) {

//                    autoDetectActivityClass = HandMoveActivity.class;

                    //posation = mActivityList.size();//设置手动检测的位置

                } else {

                    return null;
                }

            }

            Intent intent = new Intent(context, autoDetectActivityClass);
            intent.putExtra(MainActivityApp.EXTRA_ACTION, true);
            intent.putExtra(MainActivityApp.EXTRA_POSATION, posation);
            return intent;
        }

        return null;
    }


    /**
     * 播放声音 不能同时播放多种音频
     * 消耗资源较大
     *
     * @param rawId
     */
    public static void playSoundByMedia(Context context, int rawId) {
        try {

            AudioManager am =
                    (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);

            am.setStreamVolume(
                    AudioManager.STREAM_MUSIC,
                    am.getStreamMaxVolume(AudioManager.STREAM_MUSIC),
                    0);

            MediaPlayer mediaPlayer = MediaPlayer.create(context, rawId);
            mediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
            mediaPlayer.setOnCompletionListener(beepListener);
            mediaPlayer.reset();
            AssetFileDescriptor file = context.getResources().openRawResourceFd(
                    rawId);
            mediaPlayer.setDataSource(file.getFileDescriptor(),
                    file.getStartOffset(), file.getLength());
            file.close();
            mediaPlayer.setVolume(100f, 100f);
            mediaPlayer.prepare();
            mediaPlayer.start();



        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    static MediaPlayer getMediaPlayer(Context context) {

        MediaPlayer mediaplayer = new MediaPlayer();

        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.KITKAT) {
            return mediaplayer;
        }

        try {
            Class<?> cMediaTimeProvider = Class.forName("android.media.MediaTimeProvider");
            Class<?> cSubtitleController = Class.forName("android.media.SubtitleController");
            Class<?> iSubtitleControllerAnchor = Class.forName("android.media.SubtitleController$Anchor");
            Class<?> iSubtitleControllerListener = Class.forName("android.media.SubtitleController$Listener");

            Constructor constructor = cSubtitleController.getConstructor(new Class[]{Context.class, cMediaTimeProvider, iSubtitleControllerListener});

            Object subtitleInstance = constructor.newInstance(context, null, null);

            Field f = cSubtitleController.getDeclaredField("mHandler");

            f.setAccessible(true);
            try {
                f.set(subtitleInstance, new Handler());
            } catch (IllegalAccessException e) {
                return mediaplayer;
            } finally {
                f.setAccessible(false);
            }

            Method setsubtitleanchor = mediaplayer.getClass().getMethod("setSubtitleAnchor", cSubtitleController, iSubtitleControllerAnchor);

            setsubtitleanchor.invoke(mediaplayer, subtitleInstance, null);
            //Log.e("", "subtitle is setted :p");
        } catch (Exception e) {
        }

        return mediaplayer;
    }

    private static final MediaPlayer.OnCompletionListener beepListener = new MediaPlayer.OnCompletionListener() {
        public void onCompletion(MediaPlayer mediaPlayer) {
            mediaPlayer.seekTo(0);
        }
    };


    public static void startCall(Context context, String tel) {

/*
        Intent phoneIntent = new Intent("android.intent.action.CALL", Uri.parse("tel:" + 112));

        phoneIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivityForResult(phoneIntent, mRequestCode);
*/

/*   Intent callIntent = new Intent(Intent.ACTION_DIAL);
        callIntent.setData(Uri.parse("tel:" + Uri.encode("122")));
        callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivityForResult(callIntent, mRequestCode);*/

        Intent callIntent;
        if (Build.PRODUCT.contains("htc")) {
            callIntent = new Intent(Intent.ACTION_DIAL);
            callIntent.setData(Uri.parse("tel:" + Uri.encode(tel)));
            callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(callIntent);

        } else {

            try {
                callIntent = new Intent(Intent.ACTION_DIAL);
                callIntent.setData(Uri.parse("tel:" + Uri.encode(tel)));
                callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(callIntent);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

    }

}
